package com.jdy.hotel.check;

import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.jdy.hotel.check.BaseCheck.EmailCheck;
import com.jdy.hotel.check.BaseCheck.EqualCheck;
import com.jdy.hotel.check.BaseCheck.NullCheck;
import com.jdy.hotel.check.BaseCheck.PhoneCheck;
import com.jdy.hotel.check.Check.CheckView;

import java.util.ArrayList;
import java.util.List;

public class CheckHelper {

    private static CheckHelper instance;

    private final CheckView checkView;

    /**
     * 检查项目的临时容器
     */
    private List<CheckItem> checkItemContainer = new ArrayList<>();

    private CheckItem currentItem = null;

    enum CheckEnum {
        NotNull() {
            @Override
            public Check getCheck() {
                return notNullCheck;
            }
        },

        Phone() {
            @Override
            public Check getCheck() {
                return phoneCheck;
            }
        },

        Email() {
            @Override
            public Check getCheck() {
                return emailCheck;
            }
        },

        Equal() {
            @Override
            public Check getCheck() {
                return equalCheck;
            }
        };

        /**
         * 用来扩展的对象
         */
        public abstract Check getCheck();

        private final static Check notNullCheck = new NullCheck();
        private final static Check phoneCheck = new PhoneCheck();
        private final static Check emailCheck = new EmailCheck();
        private final static Check equalCheck = new EqualCheck();
    }

    public interface Checkable {

        void addCheck(CheckEnum check);

        boolean check();
    }


    private CheckHelper(CheckView checkView) {
        this.checkView = checkView;
    }

    public static CheckHelper of(CheckView checkView) {
        if (checkView == null) throw new NullPointerException("CheckView can't be null");

        if (instance == null || instance.checkView != checkView)
            instance = new CheckHelper(checkView);

        return instance;
    }

    /**
     * 绑定EditText控件
     *
     * @param fieldName  字段
     * @param promptName 字段名称
     * @param editText   控件
     * @return 当前对象
     */
    public CheckHelper edit(String fieldName, String promptName, EditText editText) {
        return bind(new CheckItem(fieldName, promptName, editText));
    }

    public CheckHelper editTake(String fieldName, String promptName, EditText editText) {
        return take(new CheckItem(fieldName, promptName, editText));
    }


    /**
     * 绑定TextView控件
     *
     * @param fieldName  字段
     * @param promptName 字段名称
     * @param textView   控件
     * @return 当前对象
     */
    public CheckHelper text(String fieldName, String promptName, TextView textView) {
        return bind(new CheckItem(fieldName, promptName, textView));
    }

    /**
     * 绑定EditText控件
     *
     * @param fieldName   字段
     * @param promptName  字段名称
     * @param radioButton 控件
     * @return 当前对象
     */
    public CheckHelper radio(String fieldName, String promptName, RadioButton radioButton) {
        return bind(new CheckItem(fieldName, promptName, radioButton));
    }

    private CheckHelper bind(CheckItem checkItem) {
        currentItem = checkItem;
        checkView.addItem(checkItem);
        return this;
    }

    private CheckHelper take(CheckItem checkItem) {
        checkItemContainer.add(checkItem);
        return bind(checkItem);
    }

    public CheckHelper notNull() {
        return registerCheck(CheckEnum.NotNull);
    }

    public CheckHelper phone() {
        return registerCheck(CheckEnum.Phone);
    }

    public CheckHelper email() {
        return registerCheck(CheckEnum.Email);
    }

    /**
     * 结束操作
     *
     * @param promptName 提示名称
     * @param editText   控件
     */
    public void equal(String promptName, EditText editText) {
        String fieldName = Check.EQUAL_PREFIX + currentItem.getFieldName();
        CheckItem checkItem = new CheckItem(fieldName, promptName, editText);
        for (Check check : currentItem.getChecks()) {
            checkItem.addCheck(check);
        }
        EqualCheck equalCheck = (EqualCheck) CheckEnum.Equal.getCheck();
        equalCheck.setTargetItem(currentItem);
        checkItem.addCheck(equalCheck);
        checkView.addItem(checkItem);
    }

    private CheckHelper registerCheck(CheckEnum checkEnum) {
        if (checkItemContainer.isEmpty()) {
            currentItem.addCheck(checkEnum);
        } else {
            for (CheckItem checkItem : checkItemContainer) {
                checkItem.addCheck(checkEnum);
            }
            checkItemContainer.clear();
        }
        return this;
    }
}
